﻿namespace Vibstudio.NetConfigurator.UI.Util
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.IO;

    public static class StarterProcess
    {
        const int ERROR_FILE_NOT_FOUND = 2;
        const int ERROR_ACCESS_DENIED = 5;

        public static void OpenFile(string fileName)
        {
            string executablePath = Directory.GetCurrentDirectory();
            string filePath = Path.Combine(executablePath, fileName);

            try
            {
                if (!File.Exists(filePath))
                    throw new FileNotFoundException(fileName, executablePath);

                Process.Start("notepad.exe", filePath);
            }
            catch (Win32Exception e)
            {
                if (e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
                    throw new Exceptions.FileNotFoundException(fileName, filePath);

                if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
                    throw new AccessViolationException(e.Message);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
    }
}
